package com.seabook.happy.website.daos;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.Category;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.seabook.happy.website.pojos.Mood;

public class MoodDao {
	private DatastoreService datastore = DatastoreServiceFactory
			.getDatastoreService();

	public boolean insertMood(Mood mood) {
		Entity moodEntity = new Entity(EntitesKind.MOOD_KIND);

		moodEntity.setProperty("moodMsg", mood.getMoodMessage());
		moodEntity.setProperty("modifiedDate", mood.getModifiedDate());
		moodEntity.setProperty("moods", mood.getMoods());

		datastore.put(moodEntity);
		return true;
	}

	public boolean updateMood(Mood mood) {
		Entity moodEntity = null;
		try {
			moodEntity = datastore.get(mood.getKey());
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		moodEntity.setProperty("moodMsg", mood.getMoodMessage());
		moodEntity.setProperty("modifiedDate", mood.getModifiedDate());
		moodEntity.setProperty("moods", mood.getMoods());

		datastore.put(moodEntity);
		return true;
	}
	
	public Key queryMoodByMessage(String moodMsg) {
		Query q = new Query(EntitesKind.MOOD_KIND);
		q.addFilter("moodMsg", Query.FilterOperator.EQUAL, moodMsg);
		PreparedQuery pq = datastore.prepare(q);
		return pq.asSingleEntity().getKey();
	}

	public boolean deleteMood(String key) {
		datastore.delete(KeyFactory.stringToKey(key));
		return true;
	}

	public List<Mood> listLatestMoods(int number) {
		Query query = new Query(EntitesKind.MOOD_KIND);
		query.addSort("modifiedDate", Query.SortDirection.DESCENDING);
		List<Entity> moodEntites = datastore.prepare(query).asList(
				FetchOptions.Builder.withLimit(number));
		
		return transferEntity2Mood(moodEntites);
	}

	private List<Mood> transferEntity2Mood(List<Entity> moodEntites) {
		List<Mood> moods = new ArrayList<Mood>();
		for (Entity entity : moodEntites) {
			Mood mood = new Mood();
			mood.setKey(entity.getKey());
			mood.setMoodMessage((String)entity.getProperty("moodMsg"));
			mood.setModifiedDate((Date)entity.getProperty("modifiedDate"));
			mood.setMoods((Category)entity.getProperty("moods"));
			moods.add(mood);
		}
		
		return moods;
	}

	public List<Mood> listAllMoods() {
		Query query = new Query(EntitesKind.MOOD_KIND);
		query.addSort("modifiedDate", Query.SortDirection.DESCENDING);
		List<Entity> moodEntites = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
		return transferEntity2Mood(moodEntites);
	}
}
